using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserContactInfoDA {

		private const string SP_GetAll = "dbo.UserContactInfo_GetAll";
		private const string SP_GetByUserKey = "dbo.UserContactInfo_GetByUserKey";
		private const string SP_Insert = "dbo.UserContactInfo_Insert";
		private const string SP_Update = "dbo.UserContactInfo_Update";
        private const string SP_UpdateLocation = "dbo.UserContactInfo_UpdateLocation";

		public static UserContactInfoEnt CreateFromReader(IDataReader reader) {
			UserContactInfoEnt item = new UserContactInfoEnt();
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.Email = ConvertUtils.ToString(reader["Email"]);
			item.HomePhone = ConvertUtils.ToString(reader["HomePhone"]);
			item.MobiPhone = ConvertUtils.ToString(reader["MobiPhone"]);
			item.Address = ConvertUtils.ToString(reader["Address"]);
            item.CityID = ConvertUtils.ToShort(reader["CityID"]);
			item.City = ConvertUtils.ToString(reader["City"]);
            item.CountryID = ConvertUtils.ToShort(reader["CountryID"]);
			item.Country = ConvertUtils.ToString(reader["Country"]);
			item.DateModified = ConvertUtils.ToDateTime(reader["DateModified"]);
			return item;
		}

        public static List<UserContactInfoEnt> GetAll(string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserContactInfoEnt> result = new List<UserContactInfoEnt>();
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetAll)) {
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        result.Add(CreateFromReader(reader));
                    }
                }
            }
            return result;
        }

        public static UserContactInfoEnt GetByUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            UserContactInfoEnt result = null;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKey)) {
                db.AddParameter(cmd, "UserKey", userKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = CreateFromReader(reader);
                    }
                }
            }
            return result;
        }

        public static int Insert(UserContactInfoEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "Email", item.Email);
				db.AddParameter(cmd, "HomePhone", item.HomePhone);
				db.AddParameter(cmd, "MobiPhone", item.MobiPhone);
				db.AddParameter(cmd, "Address", item.Address);
                db.AddParameter(cmd, "CityID", item.CityID);
                db.AddParameter(cmd, "CountryID", item.CountryID);
				db.AddParameter(cmd, "DateModified", item.DateModified);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static int Update(UserContactInfoEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Update)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "Email", item.Email);
				db.AddParameter(cmd, "HomePhone", item.HomePhone);
				db.AddParameter(cmd, "MobiPhone", item.MobiPhone);
				db.AddParameter(cmd, "Address", item.Address);
				db.AddParameter(cmd, "DateModified", item.DateModified);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static int UpdateLocation(UserContactInfoEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_UpdateLocation)) {
                db.AddParameter(cmd, "UserKey", item.UserKey);
                db.AddParameter(cmd, "CityID", item.CityID);
                db.AddParameter(cmd, "CountryID", item.CountryID);
                db.AddParameter(cmd, "DateModified", item.DateModified);
                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }

	}
}